package edu.ucsd.cs110.mycity.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

import edu.ucsd.cs110.mycity.ChatActivity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class ChatController {
	
	private static Map<String,ArrayList<String>> chatLogs = new TreeMap<String,ArrayList<String>>();
	
	public static void refresh() {
		if ( XMPPController.isLoggedIn() ){
			Collection<String> roster = XMPPController.getBuddies();
			Iterator<String> users = roster.iterator();
			while(users.hasNext()) {
				String key = users.next();
				if ( !getChatLogs().containsKey(key) ) {
					getChatLogs().put(key, new ArrayList<String>());
				}
			}
		}
	}

	public static void setChatLogs(Map<String,ArrayList<String>> chatLogs) {
		ChatController.chatLogs = chatLogs;
	}

	public static Map<String,ArrayList<String>> getChatLogs() {
		return chatLogs;
	}
	
	public static void addToLog(String user,String text) {
		Log.i("ChatController",user + " messaged " + text);
		ArrayList<String> log = get(user);
		log.add(text);
	}
	
	public static void doChat(Activity act,String username) {
		Intent chatRoom = new Intent(act, ChatActivity.class);
		chatRoom.putExtra("to",username);
		act.startActivity(chatRoom);
	}
	
	public static ArrayList<String> get(String key) {
		if ( !chatLogs.containsKey(key) ){
			chatLogs.put(key, new ArrayList<String>());
		}
		return chatLogs.get(key);
	}
	
}
